﻿<template>
    <div class="ProducerDetail">
        <MyDialog :model-value="true" :type="DialogType.C1" :title="title" @close="onClose">
            <el-form ref="ProducerDetailFormRef" label-width="80" :model="thisData" :rules="rules">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="thisData.name" maxlength="200"></el-input>
                </el-form-item>

                <el-form-item label="联系人" prop="linkName">
                    <el-input v-model="thisData.linkName" maxlength="50"></el-input>
                </el-form-item>

                <el-form-item label="联系电话" prop="linkPhone">
                    <el-input v-model="thisData.linkPhone" maxlength="20"></el-input>
                </el-form-item>

                <el-form-item label="地址" prop="address">
                    <el-input v-model="thisData.address" maxlength="200"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="thisData.email" maxlength="100"></el-input>
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input v-model="thisData.remark" maxlength="500"></el-input>
                </el-form-item>

                <el-form-item label="数据归属" prop="selectDepartmentIDs">
                    <DepartmentIds v-model="thisData.selectDepartmentIDs"></DepartmentIds>
                </el-form-item>
            </el-form>

            <template #footer>
                <el-button type="primary" :loading="loading" @click="onSave">保存</el-button>
            </template>
        </MyDialog>
    </div>
</template>

<script lang="ts" setup>
import Enums from "@/gx/Enums";
import Gx from "@/gx/Gx";
import { FormRules } from "element-plus";
import { DialogType } from "@/_gx/TsEnums";

//接收父组件的传值
const producerID = inject("producerID") as Ref<number | null>;

//定义当前页面数据对象
const thisData = ref<any>({ selectDepartmentIDs: [-1] });

//loading...
const loading = ref(false);

//定义表单验证规则
const rules = ref<FormRules>({
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    selectDepartmentIDs: [{ required: true, message: "请选择数据归属", trigger: "blur" }],
});

//标题
const title = computed(() => {
    if (Gx.IsEmpty(producerID.value)) {
        return "制造商详情 - 创建";
    } else {
        return `制造商详情 - 编辑 - ${thisData.value.name}`;
    }
});

//初始化
const Init = () => {
    if (!Gx.IsEmpty(producerID.value)) {
        App.CallApi(Api.Producer.GetByID(producerID.value), {
            success: (res) => {
                thisData.value = res;
            },
        });
    }
};

//表单Ref对象
const ProducerDetailFormRef = ref(null);

//保存
const onSave = () => {
    ProducerDetailFormRef.value.validate((isOk: boolean) => {
        if (!isOk) {
            return;
        }

        let _api = null;
        let _data = thisData.value;
        if (Gx.IsEmpty(producerID.value)) {
            //添加
            _api = Api.Producer.Insert;
        } else {
            //修改
            _api = Api.Producer.Update;
        }

        loading.value = true;
        App.CallApi(_api, {
            data: _data,
            success: (res) => {
                emits("close", true);
            },
            complete: () => {
                loading.value = false;
            },
        });
    });
};

//关闭
const onClose = () => {
    emits("close", false);
};

//向上抛出的事件与方法
const emits = defineEmits(["close"]);
defineExpose({ Init });
</script>
